// 表单验证函数
function check(needReturn){
    var tel = document.getElementById("tel");
    var pwd = document.getElementById("pwd");
    var telValue = tel.value;
    var pwdValue = pwd.value;
    var telReg = /^1[3-9][0-9]{9}$/;
    var telRight = false;
    var pwdRight = false;

    if(telValue.length==0){
        error1.innerHTML = "此项必填";
        telRight = false;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        telRight = false;
    }else{
        error1.innerHTML = "";
        telRight = true;
    }

    if(pwdValue.length==0){
        error2.innerHTML = "此项必填";
        pwdRight = false;
    }else if(pwdValue.length<6){
        error2.innerHTML = "密码长度必须6位以上";
        pwdRight = false;
    }else{
        error2.innerHTML = "";
        pwdRight = true;
    }

    if(needReturn) {
        return telRight && pwdRight;
    }
}

//1.当点击继续按钮时
loginbtn.onclick = function(){
    if(!check(true)){
        return;
    }

    //console.log(loginbtn.getElementsByClassName("btnloading")[0]);
    loginbtn.getElementsByClassName("btnloading")[0].style.display = "block";

    $.ajax({
        type:"POST",
        url:baseurl+"/bijiuser/login",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify({
            tel:tel.value,
            pwd:pwd.value
        }),
        success:function(data){
            if(data){
                localStorage.currentUser = JSON.stringify(data);
                location = "index.html";
            }else{
                tip.innerHTML = "请检查您的手机号或密码，登录失败！"
                loginbtn.getElementsByClassName("btnloading")[0].style.display = "none";
            }
        }
    });

    //location = "index.html";
};

//2.即时验证,onkeyup执行的函数是不需要返回值的
//所有直接调用check,没有给needReturn赋值，即为undefined
//就表示false,所以在33行就不会执行返回语句
tel.onkeyup = check;
pwd.onkeyup = check;


